home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------*/
- /* */
- /* TEXTWIN.CPP */
- /* */
- /* Implements non-inline functions from TEXTWIN.H */
- /* */
- /*------------------------------------------------------------------------*/
-
- #include "textwin.h"
-
- /*------------------------------------------------------------------------*/
- /* */
- /* Member functions and static member data of Dispatcher */
- /* */
- /*------------------------------------------------------------------------*/
-
- Dispatcher *Dispatcher::Window = 0;
-
- LONG FAR PASCAL _export
- Dispatcher::Callback( HWND hnd, UINT msg, UINT w, LONG l )
- {
- LONG res;
- if( !Window->Dispatch( msg, w, l, res ) )
- return DefWindowProc( hnd, msg, w, l );
- else
- return res;
- }
-
- /*------------------------------------------------------------------------*/
- /* */
- /* Member functions and static member data of WinData */
- /* */
- /*------------------------------------------------------------------------*/
-
- BI_SVectorImp<DispatchRecord<WinData> > WinData::Map( 10, 5 );
-
- int WinData::Dispatch( UINT msg, UINT w, LONG l, LONG& res )
- {
- unsigned loc = Map.find( DispatchRecord<WinData>( msg, VoidType, 0 ) );
- if( loc == UINT_MAX )
- return 0;
- else
- {
- DispatchRecord<WinData> entry = Map[loc];
- DispatchRecord<WinData>::VoidFunc proc = entry.Proc;
- switch( entry.Type )
- {
- case VoidType:
- res = (this->*(DispatchRecord<WinData>::VoidFunc)proc)();
- break;
- case UUType:
- res = (this->*(DispatchRecord<WinData>::UUFunc)proc)( LOWORD(l),
- HIWORD(l) );
- break;
- case UType:
- res = (this->*(DispatchRecord<WinData>::UFunc)proc)( w );
- break;
- }
- return 1;
- }
- }
-
- long WinData::OnSize( unsigned w, unsigned h )
- {
- WinHeight = h;
- WinWidth = w;
- Resized();
- return 0;
- }
-
- void WinData::SetHandle( HWND handle)
- {
- Handle = handle;
- if( Handle == 0 )
- return;
- HDC hdc = GetDC( Handle );
- TEXTMETRIC tm;
- GetTextMetrics( hdc, &tm );
- CharHeight = tm.tmHeight + tm.tmExternalLeading;
- CharWidth = tm.tmAveCharWidth;
- ReleaseDC( Handle, hdc );
- }
-
- /*------------------------------------------------------------------------*/
- /* */
- /* Member functions and static member data of TextWindow */
- /* */
- /*------------------------------------------------------------------------*/
-
- int TextWindow::MaxWidth = FindWidest();
-
- BI_SVectorImp<DispatchRecord<TextWindow> > TextWindow::Map( 10, 5 );
-
- int TextWindow::Dispatch( UINT msg, UINT w, LONG l, LONG& res )
- {
- unsigned loc = Map.find( DispatchRecord<TextWindow>( msg, VoidType, 0 ) );
- if( loc == UINT_MAX )
- {
- if( WinData::Dispatch( msg, w, l, res ) )
- return 1;
- else
- return 0;
- }
- else
- {
- DispatchRecord<TextWindow> entry = Map[loc];
- DispatchRecord<TextWindow>::VoidFunc proc = entry.Proc;
- switch( entry.Type )
- {
- case VoidType:
- res = (this->*(DispatchRecord<TextWindow>::VoidFunc)proc)();
- break;
- case UUType:
- res = (this->*(DispatchRecord<TextWindow>::UUFunc)proc)( LOWORD(l),
- HIWORD(l) );
- break;
- case UType:
- res = (this->*(DispatchRecord<TextWindow>::UFunc)proc)( w );
- break;
- }
- return 1;
- }
- }
-
- int TextWindow::RegisterClass( HWND instance )
- {
- WNDCLASS wndClass;
- wndClass.style = CS_HREDRAW | CS_VREDRAW;
- wndClass.lpfnWndProc = &Dispatcher::Callback;
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hInstance = instance;
- wndClass.hIcon = LoadIcon( 0, IDI_APPLICATION );
- wndClass.hCursor = LoadCursor( 0, IDC_ARROW );
- wndClass.hbrBackground = GetStockObject( WHITE_BRUSH );
- wndClass.lpszMenuName = 0;
- wndClass.lpszClassName = "TextWindow";
- return ::RegisterClass( &wndClass );
- }
-
- int TextWindow::CreateWindow( HWND instance, DWORD Flags )
- {
- SetHandle( ::CreateWindow( "TextWindow",
- "Text Sample",
- WS_OVERLAPPEDWINDOW | Flags,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- 0, 0,
- instance,
- 0 ) );
- return GetHandle() != 0;
- }
-
- void TextWindow::Create( HWND instance, int show, DWORD flags )
- {
- if( !RegisterClass( instance ) || !CreateWindow( instance, flags ) )
- return;
- ShowWindow( GetHandle(), show );
- UpdateWindow( GetHandle() );
- }
-
- char *TextWindow::Text[] =
- {
- "Give thy thoughts no tongue,",
- "Nor any unproportion'd thought his act.",
- "Be thou familiar, but by no means vulgar;",
- "The friends thou hast, and their adoption tried,",
- "Grapple them to thy soul with hoops of steel;",
- "But do not dull thy palm with entertainment",
- "Of each new-hatch'd unfledg'd comrade. Beware",
- "Of entrance to a quarrel, but being in,",
- "Bear't that the opposed may beware of thee.",
- "Give every man thine ear, but few thy voice;",
- "Take each man's censure, but reserve thy judgement.",
- "Costly thy habit as thy purse can buy,",
- "But not express'd in fancy; rich, not gaudy;",
- "For the apparel oft proclaims the man,",
- "And they in France of the best rank and station",
- "Are most select and generous, chief in that.",
- "Neither a borrower nor a lender be;",
- "For loan oft loses both itself and friend,",
- "And borrowing dulls the edge of husbandry.",
- "This above all: to thine own self be true,",
- "And it must follow, as the night the day,",
- "Thou canst not then be false to any man.",
- " W. Shakespeare,",
- " \"Hamlet, Prince of Denmark\",",
- " Act 1, Scene 4."
- };
-
- int TextWindow::Lines()
- {
- // WARNING: do not move this function. It must follow the
- // definition of TextWindow::Text, so that sizeof() can be
- // used to determine how much text is actually present.
- return sizeof(Text)/sizeof(*Text);
- }
-
- int TextWindow::FindWidest()
- {
- int widest = 0;
- for( int i = 0; i < Lines(); i++ )
- {
- int width = strlen( Text[i] );
- if( width > widest )
- widest = width;
- }
- return widest;
- }
-
- long TextWindow::OnPaint()
- {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint( GetHandle(), &ps );
- int XPos = GetCharWidth()*(1 - GetXPos() );
- for( int i = 0; i < Lines(); i++ )
- TextOut( hdc,
- XPos, GetCharHeight()*(i+1-GetYPos()),
- Text[i], strlen(Text[i]) );
- EndPaint( GetHandle(), &ps );
- return 0;
- }
-
- long TextWindow::OnDestroy()
- {
- PostQuitMessage(0);
- return 0;
- }
-
-